/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.calculator;

import java.awt.GridBagLayout;

import javax.swing.JLabel;

import net.abbp.report.model.ReportModel;
import net.abbp.swing.layout.LayoutUtils;

import com.reportgear.core.swing.pane.base.JBasePane;
import com.reportgear.report.parameter.model.Parameter;

/**
 * 
 * 报表核心-计算器-参数计算
 * 
 * @version 1.0 2011-4-10
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ParameterCalculator extends AbstractCalculator {
	public ParameterCalculator() {

	}

	@Override
	public ReportModel calculate(ReportModel model, CalcParameter parameter) {
		if (logger.isDebugEnabled()) {
			logger.debug("计算模型：参数计算");
		}

		ReportModel calcModel = model;

		JParameterInputPane inputPane = new JParameterInputPane();
		if (!model.getParameters().isEmpty()) {
			for (Parameter para : model.getParameters()) {
				inputPane.addParameter(para);
			}

			inputPane.showDialog(null, "参数输入", 320, 76 + 32 * model.getParameters().size());
		}

		return calcModel;
	}

	private class JParameterInputPane extends JBasePane {
		/**
		 * UID
		 */
		private static final long serialVersionUID = 2870618345267201954L;
		private int itemCount = 0;

		private JParameterInputPane() {
			this.setLayout(new GridBagLayout());
		}

		private void addParameter(Parameter parameter) {
			this.add(new JLabel(parameter.getCaption()), LayoutUtils.newGridBagConstraints(0, itemCount, 1, 1));
			this.add(parameter.getInputComponent(), LayoutUtils.newGridBagConstraints(1, itemCount, 1, 1));

			this.itemCount++;
		}
	}
}
